package com.zx.mes.upms.hyl.rest;

import com.zx.mes.hyl.common.msg.BaseResponse;
import com.zx.mes.hyl.common.msg.ObjectRestResponse;
import com.zx.mes.hyl.common.rest.BaseController;
import com.zx.mes.upms.hyl.biz.RoleBiz;
import com.zx.mes.upms.hyl.biz.RoleMenuBiz;
import com.zx.mes.upms.hyl.biz.RoleResourceBiz;
import com.zx.mes.upms.hyl.biz.UserRoleBiz;
import com.zx.mes.upms.hyl.entity.Role;
import com.zx.mes.upms.hyl.entity.RoleMenu;
import com.zx.mes.upms.hyl.entity.RoleResource;
import com.zx.mes.upms.hyl.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * 
 *
 * @author hyl
 * @email 632105841@qq.com
 * @date 2018-04-08 15:10:44
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController<RoleBiz,Role> {

    public static final String SPLIT_REGEX = ",";
    public static final String ANOTHER_STRING_SPANCE = "";

    @Autowired
    private RoleResourceBiz roleResourceBiz;

    @Autowired
    private RoleMenuBiz roleMenuBiz;

    @Autowired
    private UserRoleBiz userRoleBiz;

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/getResourceByRoleId")
    @ResponseBody
    public ObjectRestResponse<Role> getResourceByRoleId(String id) {
        return new ObjectRestResponse<>().rel(true).data(baseBiz.getResourceByRoleId(id));
    }

    @PostMapping("/insertRoleIdAndResourceId")
    @ResponseBody
    public ObjectRestResponse<Role> insertRoleIdAndResourceId(String roleId,String resourceIds){
        ObjectRestResponse<Role> response = new ObjectRestResponse<>();
        if (roleId !=null){
            String[] resourceId = resourceIds.substring(0, resourceIds.length()).split(SPLIT_REGEX);
//                System.out.println(resourceId);
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            //先删除后操作
            roleResourceBiz.delete(roleResource);
            if (resourceIds !=null){
                if (resourceIds.length()>3){
                    for (String id:resourceId) {
                        roleResource.setId(UUID.randomUUID().toString());
                        roleResource.setResourceId(id);
                        roleResourceBiz.insert(roleResource);

                    }

                }
            }


        }
        response.rel(true).setMessage("添加成功!");
        return response;
    }

    @GetMapping("/getMenuByRoleId")
    @ResponseBody
    public ObjectRestResponse<Role> getMenuByRoleId(String id){
        return new ObjectRestResponse<>().rel(true).data(baseBiz.getMenuByRoleId(id));
    }

    @PostMapping("/insertRoleAndMenu")
    @ResponseBody
    public BaseResponse insertRoleAndMenu(String roleId, String menuIds){
        BaseResponse response = new BaseResponse();
        if (roleId !=null){
            String[] menuId = menuIds.split(SPLIT_REGEX);
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenuBiz.delete(roleMenu);

            if ( menuIds !=null && !menuIds.equalsIgnoreCase(ANOTHER_STRING_SPANCE)){
                for (String id:menuId){
                    roleMenu.setMenuId(id);
                    roleMenu.setId(UUID.randomUUID().toString());
                    roleMenuBiz.insert(roleMenu);
                }
            }

        }
        response.setMessage("插入成功!");
        return response;
    }

    @Override
    public ObjectRestResponse<Role> remove(@PathVariable String id) {

        RoleMenu roleMenu = new RoleMenu();
        RoleResource roleResource = new RoleResource();
        UserRole userRole = new UserRole();

        // 清除中间表 角色与菜单
        roleMenu.setRoleId(id);
        // 清除中间表 角色与资源
        roleResource.setRoleId(id);

        // 清除中间表 角色与用户
        userRole.setRoleId(id);

        // 删除角色时中间表的一些信息先清除
        roleMenuBiz.delete(roleMenu);
        roleResourceBiz.delete(roleResource);
        userRoleBiz.delete(userRole);

        return super.remove(id);
    }
}